package binary_tree.LeetCode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

//class TreeNode {
//    int val;
//    binary_tree.LeetCode.TreeNode left;
//    binary_tree.LeetCode.TreeNode right;
//    TreeNode() {}
//    TreeNode(int val) { this.val = val; }
//    TreeNode(int val, binary_tree.LeetCode.TreeNode left, binary_tree.LeetCode.TreeNode right) {
//        this.val = val;
//        this.left = left;
//        this.right = right;
//    }
//}
public class Num102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> level=new ArrayList<>();
         int size= queue.size();
         for(int i=0;i<size;i++){
             TreeNode cur=queue.poll();
             level.add(cur.val);
             if(cur.left!=null){
                 queue.offer(cur.left);
             }
             if(cur.right!=null){
                 queue.offer(cur.right);
             }
         }
         res.add(level);
        }
        return res;
    }
    }

